package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.service.admin.MarketBrandService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author：Katherine
 * @Package：com.cskaoyan.market.controller
 * @Project：java-project1
 * @name：AdminBrandController
 * @Date：2023/5/7 8:47
 * @Filename：AdminBrandController
 */
@RequiresAuthentication
@RestController
@RequestMapping("/admin/brand")
@Validated
public class AdminBrandController {
    private final Log logger = LogFactory.getLog(AdminBrandController.class);

    @Autowired
    MarketBrandService marketAdminService;


    //list是get请求所以写这里
    @RequiresPermissions("admin:brand:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String id, String name) {

        List<MarketBrand> marketBrands = marketAdminService.list(id, name);
        if (marketBrands.size() == 0) {
            //这次查询获取数据失败
            return ResponseUtil.fail();

        }

        ArrayList<Object> list = new ArrayList<>();
        for (MarketBrand marketBrand : marketBrands) {
            //不能把它放在上面，否则只有一条数据
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", marketBrand.getId());
            map.put("name", marketBrand.getName());
            map.put("desc", marketBrand.getDesc());
            map.put("picUrl", marketBrand.getPicUrl());
            map.put("sortOrder", marketBrand.getSortOrder());
            map.put("floorPrice", marketBrand.getFloorPrice());
            map.put("addTime", marketBrand.getAddTime());
            map.put("updateTime", marketBrand.getUpdateTime());
            list.add(map);
        }

        return ResponseUtil.okList(list);
    }

    // {"id":1046006,"name":"22","desc":"11","floorPrice":2,"addTime":"2023-05-23 10:56:52","updateTime":"2023-05-23 10:56:52"}
    @RequiresPermissions("admin:brand:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody MarketBrand brand) {

        //获取数据
        Integer id = brand.getId();

        int marketBrands = marketAdminService.delete(id);

        if (marketBrands == 0) {
            //这次添加数据失败
            return ResponseUtil.fail();

        }
        return ResponseUtil.ok();
    }


    @RequiresPermissions("admin:brand:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody MarketBrand brand)  {
        // {"id":1046006,"name":"22","desc":"11","floorPrice":2,"addTime":"2023-05-23 10:56:52","updateTime":"2023-05-23 10:56:52"}

        List<MarketBrand> marketBrands = marketAdminService.update(brand);

        if (marketBrands.size() == 0) {
            //这次添加数据失败
            return ResponseUtil.fail();
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("name", marketBrands.get(0).getId());
        map.put("desc", marketBrands.get(0).getDesc());
        map.put("picUrl", marketBrands.get(0).getFloorPrice());
        map.put("floorPrice", marketBrands.get(0).getFloorPrice());

        //封装响应体
        return ResponseUtil.ok(map);
    }

    @RequiresPermissions("admin:brand:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody MarketBrand brand) {
        // {"name":"222","desc":"222","floorPrice":"222","picUrl":"http://101.43.69.31:8083/wx/storage/fetch/afr9p7g1anfpx1130zbl.jpg"}
        //获取数据
        String name = brand.getName();
        String desc = brand.getDesc();
        BigDecimal floorPrice = brand.getFloorPrice();
        String picUrl = brand.getPicUrl();

        //点住create然后alt+enter在服务层MarketBrandService生成接口
        //这里是分层了，控制层的活儿干完了交给下一层去做。
        List<MarketBrand> marketBrands = marketAdminService.create(name, desc, floorPrice,picUrl);

        if (marketBrands.size() == 0) {
            //这次添加数据失败
            return ResponseUtil.fail();
        }

        //不能把它放在上面，否则只有一条数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", marketBrands.get(0).getId());
        map.put("name", marketBrands.get(0).getId());
        map.put("desc", marketBrands.get(0).getDesc());
        map.put("floorPrice", marketBrands.get(0).getFloorPrice());
        map.put("addTime", marketBrands.get(0).getAddTime());
        map.put("updateTime", marketBrands.get(0).getUpdateTime());

        //封装响应体
        return ResponseUtil.ok(map);
    }
}



